﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using MvcAtletIS.Helpers;
using MvcAtletIS.Models;

namespace MvcAtletIS.Areas.Admin.Controllers
{
    [Authorize(Roles = "Administrators")]
    public class LinksController : Controller
    {
        private atletisEntities db = new atletisEntities();

        // GET: /Admin/Links/
        public ViewResult Index(int CategoryId)
        {
            ViewBag.ParentCategory = db.LinksCategories.SingleOrDefault(lc => lc.ID == CategoryId);
            var links = db.Links.Include("LinksCategories").Where(l => l.CategoryId == CategoryId).OrderBy(l => l.Order);
            return View(links.ToList());
        }

        // GET: /Admin/Links/Details/5
        public ViewResult Details(int id)
        {
            Links links = db.Links.Single(l => l.ID == id);
            return View(links);
        }

        // GET: /Admin/Links/Create
        public ActionResult Create(int CategoryId)
        {
            Links model = new Links();
            ViewBag.LogoPath = FilesHelper.GetFiles(Server.MapPath("~/Content/logos"), string.Empty);
            model.CategoryId = CategoryId;
            return View(model);
        } 

        // POST: /Admin/Links/Create
        [HttpPost]
        public ActionResult Create(Links links)
        {
            if (ModelState.IsValid)
            {
                db.Links.AddObject(links);
                db.SaveChanges();
                return RedirectToAction("Index", new { CategoryId = links.CategoryId });  
            }

            return View(links);
        }
        
        // GET: /Admin/Links/Edit/5
        public ActionResult Edit(int id)
        {
            Links links = db.Links.Single(l => l.ID == id);
            ViewBag.LogoPath = FilesHelper.GetFiles(Server.MapPath("~/Content/logos"), links.LogoPath);
            return View(links);
        }

        // POST: /Admin/Links/Edit/5
        [HttpPost]
        public ActionResult Edit(Links links)
        {
            if (ModelState.IsValid)
            {
                db.Links.Attach(links);
                db.ObjectStateManager.ChangeObjectState(links, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index", new { CategoryId = links.CategoryId });
            }
            return View(links);
        }

        // GET: /Admin/Links/Delete/5
        public ActionResult Delete(int id)
        {
            Links links = db.Links.Single(l => l.ID == id);
            return View(links);
        }

        // POST: /Admin/Links/Delete/5
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            Links links = db.Links.Single(l => l.ID == id);
            int categoryId = Convert.ToInt32(links.CategoryId);

            db.Links.DeleteObject(links);
            db.SaveChanges();
            return RedirectToAction("Index", new { CategoryId = categoryId });
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }

        



    }
}